package feizhou.untils.tools;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class ReflectTools {

    /**
     * 设置对象的属性值
     *
     * @author <a href="920786312@qq.com">周飞</a>
     * @since 2021/11/3 10:53
     */

    public static void setValue(Object obj, String fieldName, Object value) {

        Class<?> clazz = obj.getClass();
        try {
            Field field = clazz.getDeclaredField(fieldName);
            field.setAccessible(true);
            field.set(obj, value);
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取对象的属性值
     *
     * @author <a href="920786312@qq.com">周飞</a>
     * @since 2021/11/3 10:53
     */

    public static Object getValue(Object obj, String fieldName) {

        Class<?> clazz = obj.getClass();
        try {
            Field field = clazz.getDeclaredField(fieldName);
            field.setAccessible(true);
            return field.get(obj);
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Object getValue(Object obj, Class<? extends Annotation> annotationClass) {

        List<Field> fields = getFields(obj);
        for (Field field : fields) {
            if (field.isAnnotationPresent(annotationClass)) {
                field.setAccessible(true);
                try {
                    return field.get(obj);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

    public static List<Field> getFields(Object obj) {
        Class<?> clazz = obj.getClass();
        Field[] currentFields = clazz.getDeclaredFields();
        // 排除序列化字段
        List<Field> fieldList = Arrays.stream(currentFields).filter(f -> !"serialVersionUID".equalsIgnoreCase(f.getName())).distinct().collect(Collectors.toList());
        return fieldList;
    }

    public static Map<String, Object> objectToMap(Object obj) {
        Map<String, Object> map = new LinkedHashMap<>();

        Class<?> clazz = obj.getClass();
        Field[] currentFields = clazz.getDeclaredFields();
        // 排除序列化字段
        List<Field> fieldList = Arrays.stream(currentFields).filter(f -> !"serialVersionUID".equalsIgnoreCase(f.getName())).distinct().collect(Collectors.toList());

        for (Field field : fieldList) {
            field.setAccessible(true);
            try {
                map.put(field.getName(), field.get(obj));
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        return map;
    }

    public static <T> T mapToObject(Map<String, Object> map, T t) {
        if (map == null) return null;
        Object obj = null;
        try {
            obj = t.getClass().newInstance();

            Field[] fields = obj.getClass().getDeclaredFields();
            for (Field field : fields) {
                int mod = field.getModifiers();
                if (Modifier.isStatic(mod) || Modifier.isFinal(mod)) {
                    continue;
                }
                field.setAccessible(true);
                field.set(obj, map.get(field.getName()));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return (T) obj;
    }
    public static <T> T linkedHashMapToObject(LinkedHashMap<String, Object> map, T t) {
        if (map == null) return null;
        Object obj = null;
        try {
            obj = t.getClass().newInstance();

            Field[] fields = obj.getClass().getDeclaredFields();
            for (Field field : fields) {
                int mod = field.getModifiers();
                if (Modifier.isStatic(mod) || Modifier.isFinal(mod)) {
                    continue;
                }
                field.setAccessible(true);
                field.set(obj, map.get(field.getName()));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return (T) obj;
    }
}